package tools;
import java.io.File;
import java.util.Arrays;

public class FileUtil {
     //for test
     public static void main(String []args) {
        File dirFile = new File("../ServerLog");
        
        File [] sortedFiles = listSortedFiles(dirFile);
                
     }
     
     public static  File[] listSortedFiles(File[] files) {
    	 FileWrapper [] fileWrappers = new FileWrapper[files.length];
         for (int i=0; i<files.length; i++) {
             fileWrappers[i] = new FileWrapper(files[i]);
         }
        
         Arrays.sort(fileWrappers);
        
        File []sortedFiles = new File[files.length];
         for (int i=0; i<files.length; i++) {
             sortedFiles[i] = fileWrappers[i].getFile();
         }
        return sortedFiles;
     }
    	 
     
     
     //list sorted files
     public static File[] listSortedFiles(File dirFile) {
     assert dirFile.isDirectory();

        File[] files = dirFile.listFiles();
        return listSortedFiles(files);
         
     }
}

     class FileWrapper implements Comparable {
     /** File */
	     private File file;
	    
	     public FileWrapper(File file) {
	         this.file = file;
	     }
	    
	     public int compareTo(Object obj) {
	         assert obj instanceof FileWrapper;
	        
	         FileWrapper castObj = (FileWrapper)obj;
	                
	         if (this.file.getName().compareTo(castObj.getFile().getName()) > 0) {
	             return 1;
	         } else if (this.file.getName().compareTo(castObj.getFile().getName()) < 0) {
	             return -1;
	         } else {
	             return 0;
	         }
	     }
	     	    
	     public File getFile() {
	         return this.file;
	     }
     }
